package com.example.demo.controller;


import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.map.MapUtil;
import com.example.demo.common.lang.Const;
import com.example.demo.common.lang.Result;
import com.example.demo.entity.SysUser;
import com.example.demo.service.SysUserService;
import com.google.code.kaptcha.Producer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.Principal;
import java.util.UUID;


@RestController
public class AuthController extends BaseController {
    @Resource
    Producer producer;

    @GetMapping("/captcha")
    public Result captcha() throws IOException {
        String key = UUID.randomUUID().toString();
        String code = producer.createText();
        //为了测试
        key = "aaaaa";
        code = "11111";
        BufferedImage image = producer.createImage(code);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(image,"jpg",outputStream);
        Base64Encoder encoder = new Base64Encoder();
        String str = "data:image/jpeg;base64,";
        String base64Img = str + encoder.encode(outputStream.toByteArray());
        redisUtil.hset(Const.CAPTCHA_KEY,key,code,120);
        return Result.succ(
                MapUtil.builder()
                        .put("token",key)
                        .put("captchaImg",base64Img)
                        .build()
        );
    }
    @GetMapping("/sys/userInfo")
   public Result  userInfo(Principal principal){
        String username = principal.getName();
        SysUser user = sysUserService.getByUsername(username);
        return Result.succ(MapUtil.builder()
                .put("id",user.getId())
                .put("username",username)
                .put("avatar",user.getAvatar())
                .put("created",user.getCreated())
                .map()

        );
    }


}
